//
//  HWSortingView.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/2.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWSortingView.h"
#import "HWPaiXuTableViewCell.h"

@interface HWSortingView()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) UIView *grayView;
@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSArray *dataAray;
@property(strong,nonatomic) NSMutableArray *selectedMuArray;


@end

@implementation HWSortingView




-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36*3) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.scrollEnabled = NO;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        
        
    }
    return   _mainaTableVie ;
}

- (instancetype)initWithFrame:(CGRect)frame  {
    
    if (self  = [super initWithFrame:frame]) {
        // 添加灰色背景
        [self addgrayView];
        
        [self setupUI];
        
        
    }
    return  self;
    
}


//  设置UI
- (void)setupUI {
    
    
    self.whiteView = [[UIView alloc]init];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.whiteView];
    
    UIView *fengexianView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
    fengexianView.backgroundColor = RGB(215, 215, 215);
    //  fengexianView.backgroundColor = [UIColor orangeColor];
    [self.whiteView addSubview:fengexianView];
    
    
    self.dataAray = @[@"默认价格(按时间顺序)", @"按价格从高到低排序",@"按价格从低到高排序"];
    self.selectedMuArray = [[NSMutableArray alloc]init];
    [self.selectedMuArray addObject:@(YES)];
    [self.selectedMuArray addObject:@(NO)];
    [self.selectedMuArray addObject:@(NO)];
  //  [self.selectedMuArray addObject:@(NO)];
    
    //添加UItableView
    [self.whiteView addSubview:self.mainaTableVie];
    


    //  全部分类
    UIButton *chongzhiBut = [UIButton buttonWithType:UIButtonTypeCustom];
    chongzhiBut.frame =   CGRectMake(0, 144-35, kScreenW/3, 35);
    chongzhiBut.backgroundColor = RGB(239, 236, 239);
    [chongzhiBut setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
    [chongzhiBut setTitle:@"重置" forState:UIControlStateNormal];
    chongzhiBut.titleLabel.font = [UIFont systemFontOfSize:12];
    [chongzhiBut addTarget:self action:@selector(chongtap) forControlEvents:UIControlEventTouchUpInside];

    [self.whiteView addSubview:chongzhiBut];


    UIButton *determineBut = [UIButton buttonWithType:UIButtonTypeCustom];
    determineBut.frame =   CGRectMake(kScreenW/3, 144-35, kScreenW/3*2, 35);
    determineBut.backgroundColor = RGB(54,147,253);
    [determineBut setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
    [determineBut setTitle:@"确定" forState:UIControlStateNormal];
    determineBut.titleLabel.font = [UIFont systemFontOfSize:12];
    [determineBut addTarget:self action:@selector(queding) forControlEvents:UIControlEventTouchUpInside];
    [self.whiteView addSubview:determineBut];





    
    
    
    
    
    
    
    
    
    
}
- (void)relayoutTheChildControls {
    // self.whiteView.backgroundColor = [UIColor redColor];
    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 36*4);
    self.grayView.frame = CGRectMake(0, self.whibackvieY+ 36*4, kScreenW, kScreenH-self.whibackvieY-36*4+10);
    
    
    
    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,300, 400, 400);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


#pragma mark --------   点击一些按钮操作
// 点击重置按钮
- (void)chongtap {
    
    DLog(@"重置按钮");
        self.selectedMuArray = [[NSMutableArray alloc]init];
       [self.selectedMuArray addObject:@(YES)];
       [self.selectedMuArray addObject:@(NO)];
       [self.selectedMuArray addObject:@(NO)];
       [self.mainaTableVie reloadData];
}

// 点击确定按钮

- (void)queding {
    
    
    DLog(@"确定");
    
  
    for (int i = 0 ; i <  self.selectedMuArray.count; i++) {
       //  - (void)HWSortingViewXuZhong:(int )tag;
        
        NSNumber *nunbe  = self.selectedMuArray[i];
        
        if ([nunbe intValue]) {
            [self.delagate HWSortingViewXuZhong:i];
        
        }
    }
    
     [self removeFromSuperview];
     
 
}




//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
     return  self.dataAray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID  = @"cellID";
    
    HWPaiXuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell  = [[NSBundle mainBundle]loadNibNamed:@"HWPaiXuTableViewCell" owner:nil options:nil].lastObject;
    }
    
    
    cell.shaxuanTiaoJianLabel.text = [NSString stringWithFormat:@"%@",self.dataAray[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSNumber *xuanzhongNum = self.selectedMuArray[indexPath.row];
    
    if ([xuanzhongNum intValue] == YES) {
        
        cell.shaxuanTiaoJianLabel.textColor = RGB(54, 147, 253);
        cell.icoImageView.hidden = NO;
    
    } else {
        
     //   cell.nameLabel.textColor = RGBACOLOR(51 , 51, 51, 1);
        cell.shaxuanTiaoJianLabel.textColor = RGB(51, 51, 51);
        cell.icoImageView.hidden = YES;
        
    }
    
    if (indexPath.row == 2) {
        cell.fengexianView.hidden = YES;
    } else {
        
         cell.fengexianView.hidden = NO;
    }
    
  
    return  cell;
    
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"xiaopingguo");
    
    //  NSNumber *number =   self.selectedMuArray[indexPath.row];
    [self.selectedMuArray removeAllObjects];
    for (int i = 0; i < self.dataAray.count; i++) {
        [self.selectedMuArray addObject: [NSNumber numberWithBool:NO]];
        [self.selectedMuArray addObject: [NSNumber numberWithBool:NO]];
      
        
    }
    
    
    [self.selectedMuArray insertObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row];
    
    [self.mainaTableVie reloadData];
    
    
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  36;
}
 

@end
